import { modal } from 'vue-strap'
import updateInvesterBuytype from '../update-invester-buy-type/update-invester-buy-type.vue'
import { addSubtype, getInvesterBetaList, showMsg } from '../../../../../vuex/action.js'

export default {
  data() {
    return {
      showAddModal: false,
      showAddBuyTypeModal: false,
      betaList: [],
      subBetaIds: [],
      params: {
        betaId: '',
        name: '',
        description: '',
        subBetaIds: '',
        status: 0,
        isList: 0,
        isHomepage: 0
      },
      pages: {
        start: 0,
        totalPages: 0,
        limit: 99999
      }
    }
  },
  computed: {
    packageParams() {
      this.params.subBetaIds = this.subBetaIds.join(',')
      this.params.type = this.payType
      return this.params
    }
  },
  methods: {
    validParams(params) {
      let key
      let val
      for (key in params) {
        val = params[key]
        if (['betaId', 'name'].indexOf(key) >= 0 && (val + '').length === 0) {
          return '* 标记的均为必填/必选项'
        }
      }
      if (this.subBetaIds.length === 0) {
        return '* 标记的均为必填/必选项'
      }
    },
    evtShowModal() {
      this.showAddModal = true
    },
    evtShowModalDialog() {
      this.xGetInvesterBetaList({
        start: this.pages.start,
        limit: this.pages.limit,
        type: this.payType
      }).then((res) => {
        this.betaList = res.data.list
        this.showAddModal = true
      })
    },
    evtAddPackage() {
      let validMsg = this.validParams(this.packageParams)
      if (validMsg) {
        return this.xShowMsg({
          type: 'danger',
          content: validMsg
        })
      }

      this.xAddSubtype(this.packageParams).then((res) => {
        this.showAddModal = false
        this.$dispatch('on_refresh_list')
      }).catch((res) => {
        this.xShowMsg({
          type: 'danger',
          content: res.error_msg
        })
      })
    },
    goCancel() {
      this.showAddModal = false
    },
    goAddBuytype() {
      this.evtAddPackage()
    },
    showAddBuytype() {
      this.$refs.addbuytype.showUpdateDialog()
    }
  },
  vuex: {
    actions: {
      xShowMsg: showMsg,
      xGetInvesterBetaList: getInvesterBetaList,
      xAddSubtype: addSubtype
    },
    getters: {
      payType: state => state.payType
    }
  },
  components: {
    'vue-modal': modal,
    'update-invester-buy-type': updateInvesterBuytype
  },
  ready() {
  }
}
